package g360.WebXpdlValidation;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


/**
 *
 * @author andya
 */
public class ValidatorProperties 
{
  public static final String DATA_PATH = "XpdlValidator.DataPath"; 
  public static final String PROPERTIES_FILE_NAME = "XpdlValidator.properties";
  private Properties properties = null;
  private String propertiesPath = null;

  public ValidatorProperties()
  {
      String separator = System.getProperty("file.separator"); //$NON-NLS-1$
      this.propertiesPath = "." + separator;
      this.properties = loadProperties(this.propertiesPath + ValidatorProperties.PROPERTIES_FILE_NAME);
  }

  /**
   * loadProperties
   *
   * @param pathToPropertiesFile String
   * @return Properties
   */
  private Properties loadProperties(String pathToPropertiesFile)
  {
    Properties properties = new Properties();
    if (pathToPropertiesFile != null)
    {
      File file = new File(pathToPropertiesFile);
      String fullPath = file.getAbsolutePath();
      if (file.exists())
      {
        try
        {
          FileInputStream stream = new FileInputStream(file);
          properties.load(stream);
        }
        catch (FileNotFoundException ex)
        {
          // CVSketch.log.debug("Error loading property file: " + pathToPropertiesFile + " File Not Found."); //$NON-NLS-1$ //$NON-NLS-2$
        }
        catch (IOException ex2)
        {
          // CVSketch.log.debug("Error loading property file: " + pathToPropertiesFile + " I/O Error."); //$NON-NLS-1$ //$NON-NLS-2$
        }
      }
    }
    return properties;
  }

  
  
  /**
   * 
   * @return
   */
  public String getDataPath(String defaultValue)
  {
    return getProperty(ValidatorProperties.DATA_PATH, defaultValue); //$NON-NLS-1$
  }

  /**
   * getBooleanProperty
   *
   * @param propertyName String
   * @param defaultValue boolean
   */
  public boolean getBooleanProperty(String propertyName, boolean defaultValue)
  {
    boolean result = defaultValue;
    String value = getProperty(propertyName, null);
    if (value != null)
    {
      Boolean valueBoolean = Boolean.valueOf(value);
      result = valueBoolean.booleanValue();
    }
    return result;
  }

  /**
   * getProperty
   *
   * @param propertyName String
   * @param defaultValue String
   * @return String
   */
  public String getProperty(String propertyName, String defaultValue)
  {
    String result = defaultValue;
    String value = null;
    if (value == null)
    {
      value = this.properties.getProperty(propertyName);
    }
    if (value != null)
    {
      result = value;
    }
    return result;
  }

  /**
   * getIntegerProperty
   *
   * @param propertyName String
   * @param defaultValue Integer
   * @return Integer
   */
  public Integer getIntegerProperty(String propertyName, Integer defaultValue)
  {
    Integer result = defaultValue;
    String value = getProperty(propertyName, null);
    if (value != null)
    {
      try
      {
        result = new Integer(value);
      }
      catch (NumberFormatException ex)
      {
        //
      }
    }
    return result;
  }

  /**
   * getIntProperty
   *
   * @param propertyName String
   * @param defaltValue int
   * @return int
   */
  public int getIntProperty(String propertyName, int defaltValue)
  {
    int result = defaltValue;
    Integer value = getIntegerProperty(propertyName, null);
    if (value != null)
    {
      result = value.intValue();
    }
    return result;
  }

  /**
   * getDoubleProperty
   *
   * @param propertyName String
   * @param defaultValue double
   * @return double
   */
  public double getDoubleProperty(String propertyName, double defaultValue)
  {
    double result = defaultValue;
    String value = getProperty(propertyName, null);
    if (value != null)
    {
      try
      {
        result = Double.parseDouble(value);
      }
      catch (NumberFormatException ex)
      {
        //
      }
    }
    return result;
  }
  
  /**
   * getColorProperty
   * 
   * 
   */
  public Color getColorProperty(String propertyName, Color defaultValue)
  {
    Color result = defaultValue;
    String value = getProperty(propertyName, null);
    if (value != null)
    {
      try
      {
        int start = 0;
        int end = 0;
        String s;
        start = value.indexOf('(');
        end = value.indexOf(',', start);
        s = value.substring(start+ 1, end);
        s = s.trim();
        int red = Integer.parseInt(s);
        start = end + 1;
        end = value.indexOf(',', start);
        s = value.substring(start, end);
        s = s.trim();
        int green = Integer.parseInt(s);
        start = end + 1;
        end = value.indexOf(')', start);
        s = value.substring(start, end);
        s = s.trim();
        int blue = Integer.parseInt(s);
        result = new Color(red, green, blue);
      }
      catch (Exception ex)
      {
        System.err.println("Error when parsing color property:  " + value); //$NON-NLS-1$
        ex.printStackTrace();
      }
    }
    return result;
  }
  
  
}
